package com.xh.bussiness.system.controller;

import com.xh.bussiness.system.bean.dto.SystemParamBean;
import com.xh.bussiness.system.service.ISystemParamService;
import com.xh.bussiness.system.bean.request.SystemParamRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

import org.springframework.stereotype.Controller;
import com.xh.core.controller.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author daiwei
 * @since 2018-09-28
 */
@Controller
@RequestMapping("/system/systemParam")
public class SystemParamController extends BaseController {

    @Autowired
    private ISystemParamService systemParamService;

    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/toList")
    public String toList(){
        return "admin/system/systemParam/list";
    }


    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/queryPage")
    @ResponseBody
    public XhPage queryPage(XhPage page, SystemParamRequest bean){
        page=systemParamService.querySystemParamPage(page,bean);
        return page;
    }


    /**
     * 导出全部
     * @param bean
     * @return
     */
    @RequestMapping(value="/exportAll",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult exportAll(SystemParamRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=systemParamService.exportAll(request,exportHeaderBeans,bean,"querySystemParamPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 前往维护页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "admin/system/systemParam/add";
    }

    /**
     * 前往维护页面
     * @return
     */
    @RequestMapping("/toModify/{id}")
    public String toModify(@PathVariable String id){
        return "admin/system/systemParam/modify";
    }

    /**
     * 前往查看页面
     * @param id
     * @return
     */
    @RequestMapping("/toView/{id}")
    public String toView(@PathVariable String id){
        System.out.println(JSONObject.toJSON(systemParamService.getById(id)));
        return "admin/system/systemParam/view";
     }

    /**
     * 新增和保存
     * @param systemParamBean 实体对象
     * @return
     */
    @RequestMapping(value="/saveOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public AjaxResult saveOrUpdate(SystemParamBean systemParamBean){
        if(systemParamBean.getId()==null){
            systemParamService.insertObj(systemParamBean);
        }else{
            systemParamService.updateObj(systemParamBean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/deleteById/{id}")
    @ResponseBody
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        systemParamService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        systemParamService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @RequestMapping("/querySystemParamByCode")
    @ResponseBody
    @Transactional
    public AjaxResult querySystemParamByCode(String code){
        SystemParamBean systemParamBean = systemParamService.queryByCode(code);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, systemParamBean);
    }
}

